import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import "../components" as Components
import "../styles" as Styles

Dialog {
    id: newTaskDialog
    title: qsTr("新建任务")
    modal: true
    standardButtons: Dialog.Ok | Dialog.Cancel

    // 从外部注入（TaskManagementScreen传入）
    property var availableTemplates: []
    property var taskViewModel: null

    // 自定义属性
    property string selectedTemplate: ""
    property alias agvNumber: agvNumberSpinBox.value

    // 信号
    signal taskAccepted(var taskData)
    signal taskRejected()

    width: 450
    height: 320

    onAccepted: {
        var taskData = {
            templateName: selectedTemplate,
            agvNumber: agvNumber === 0 ? -1 : agvNumber  // 0表示自动选择
        }
        newTaskDialog.taskAccepted(taskData)
    }

    onRejected: {
        newTaskDialog.taskRejected()
    }

    contentItem: Rectangle {
        color: Styles.Theme.background
        radius: Styles.Theme.radiusNormal

        ScrollView {
            anchors.fill: parent
            anchors.margins: Styles.Theme.spacingLarge
            clip: true

            ColumnLayout {
                width: parent.width
                spacing: Styles.Theme.spacingNormal

                // 基本信息区域
                Rectangle {
                    Layout.fillWidth: true
                    Layout.preferredHeight: basicInfoColumn.height + Styles.Theme.spacingNormal * 2
                    color: Styles.Theme.surface
                    border.color: Styles.Theme.border
                    border.width: 1
                    radius: Styles.Theme.radiusSmall

                    ColumnLayout {
                        id: basicInfoColumn
                        anchors.left: parent.left
                        anchors.right: parent.right
                        anchors.top: parent.top
                        anchors.margins: Styles.Theme.spacingNormal
                        spacing: Styles.Theme.spacingNormal

                        Text {
                            text: qsTr("任务配置")
                            color: Styles.Theme.textPrimary
                            font.pixelSize: Styles.Theme.fontSizeNormal
                            font.weight: Styles.Theme.fontWeightMedium
                        }

                        GridLayout {
                            Layout.fillWidth: true
                            columns: 2
                            rowSpacing: Styles.Theme.spacingNormal
                            columnSpacing: Styles.Theme.spacingNormal

                            Text {
                                text: qsTr("任务模板:")
                                color: Styles.Theme.textSecondary
                                font.pixelSize: Styles.Theme.fontSizeSmall
                                Layout.alignment: Qt.AlignTop
                            }

                            ComboBox {
                                id: templateCombo
                                Layout.fillWidth: true
                                Layout.minimumHeight: 48
                                model: availableTemplates
                                currentIndex: -1
                                displayText: currentIndex >= 0 ? currentText : qsTr("请选择模板...")
                                onCurrentTextChanged: {
                                    selectedTemplate = currentText
                                }
                            }

                            Text {
                                text: qsTr("执行AGV:")
                                color: Styles.Theme.textSecondary
                                font.pixelSize: Styles.Theme.fontSizeSmall
                                Layout.alignment: Qt.AlignTop
                            }

                            RowLayout {
                                Layout.fillWidth: true
                                spacing: Styles.Theme.spacingSmall

                                SpinBox {
                                    id: agvNumberSpinBox
                                    from: 0
                                    to: 999
                                    value: 0
                                    stepSize: 1
                                    Layout.preferredWidth: 120
                                }

                                Text {
                                    text: agvNumberSpinBox.value === 0 ? qsTr("(自动选择)") : qsTr("AGV-%1").arg(String(agvNumberSpinBox.value).padStart(3, '0'))
                                    color: Styles.Theme.textTertiary
                                    font.pixelSize: Styles.Theme.fontSizeSmall
                                    Layout.fillWidth: true
                                }
                            }
                        }
                    }
                }

                // 预览区域
                Rectangle {
                    Layout.fillWidth: true
                    Layout.preferredHeight: previewColumn.height + Styles.Theme.spacingNormal * 2
                    color: Styles.Theme.backgroundSecondary
                    border.color: Styles.Theme.border
                    border.width: 1
                    radius: Styles.Theme.radiusSmall

                    ColumnLayout {
                        id: previewColumn
                        anchors.left: parent.left
                        anchors.right: parent.right
                        anchors.top: parent.top
                        anchors.margins: Styles.Theme.spacingNormal
                        spacing: Styles.Theme.spacingSmall

                        Text {
                            text: qsTr("任务预览")
                            color: Styles.Theme.textPrimary
                            font.pixelSize: Styles.Theme.fontSizeNormal
                            font.weight: Styles.Theme.fontWeightMedium
                        }

                        Text {
                            text: getTaskPreview()
                            color: Styles.Theme.textSecondary
                            font.pixelSize: Styles.Theme.fontSizeSmall
                            wrapMode: Text.WordWrap
                            Layout.fillWidth: true
                        }
                    }
                }
            }
        }
    }

    // 按钮状态控制
    property bool isValid: selectedTemplate !== ""

    footer: DialogButtonBox {
        standardButtons: Dialog.Ok | Dialog.Cancel

        Component.onCompleted: {
            standardButton(Dialog.Ok).enabled = Qt.binding(function() { return isValid })
            standardButton(Dialog.Ok).text = qsTr("创建任务")
            standardButton(Dialog.Cancel).text = qsTr("取消")
        }
    }

    // 辅助函数
    function getTaskPreview() {
        if (selectedTemplate === "") {
            return qsTr("请选择任务模板")
        }

        var preview = qsTr("模板: %1\n").arg(selectedTemplate)

        if (agvNumber === 0) {
            preview += qsTr("执行AGV: 自动选择")
        } else {
            preview += qsTr("执行AGV: AGV-%1").arg(String(agvNumber).padStart(3, '0'))
        }

        return preview
    }

    // 重置对话框
    function resetForm() {
        templateCombo.currentIndex = -1
        agvNumberSpinBox.value = 0
        selectedTemplate = ""
    }

    // 打开时重置
    onOpened: {
        resetForm()
        templateCombo.forceActiveFocus()
    }
}